package com.emmy.walnut.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.emmy.walnut.R;
import com.emmy.walnut.constant.AppConstant;
import com.emmy.walnut.util.ActivityControlUtil;
import com.emmy.walnut.util.PreferenceUtil;
import com.kyleduo.switchbutton.SwitchButton;

import java.net.URI;

public class MyFragment extends Fragment {

    private ImageView avatar;
    private TextView textView;

    private Button exitButton;
    private SwitchButton nightModeButton;

    private LinearLayout exportButton;

    private Context mContext;

    private Uri uri;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_my, container, false);
        initView(view);
        return view;
    }

    private void initView(View view){
        exitButton = view.findViewById(R.id.exit);
        nightModeButton = view.findViewById(R.id.nightMode);

        avatar = view.findViewById(R.id.avatar);

        Glide.with(getContext()).load(AppConstant.avatar_path).into(avatar);

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivity(intent);
//                startActivityForResult(intent, IMAGE);
            }
        });

        textView = view.findViewById(R.id.username);
        textView.setText(AppConstant.username);

        exitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppConstant.username = "";
                PreferenceUtil.putBoolean("isLogin", false);
                PreferenceUtil.putBoolean("isNight", false);
                PreferenceUtil.putString("avatarPath","");
                PreferenceUtil.apply();
                ActivityControlUtil.finishAll();
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                startActivity(new Intent(mContext, LoginActivity.class));
            }
        });

        nightModeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PreferenceUtil.getBoolean("isNight")){
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    PreferenceUtil.putBooleanWithApply("isNight",false);
                    getActivity().recreate();
                }else{
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    PreferenceUtil.putBooleanWithApply("isNight",true);
                    getActivity().recreate();
                }
            }
        });

        exportButton = view.findViewById(R.id.export);
        exportButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ExportActivity.class));
            }
        });
    }
}
